package tomcat.Impl;

import tomcat.Cookie;
import tomcat.HttpServletResponse;

import java.io.CharArrayWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class HttpServletResponseImpl implements HttpServletResponse {

    private Socket socket;
    private OutputStream out;
    // 字符输出流 ==> 输出到内存 ==> 资源流
    private CharArrayWriter charArrayWriter = new CharArrayWriter();
    // PrintWriter 打印输出流，定义输出的格式，输出到哪里，该流不知道 ==> 处理流
    private PrintWriter printWriter = new PrintWriter(charArrayWriter);
    // LinkedHashMap ==> 有序map集合
    private Map<String,String> headerMap = new LinkedHashMap<>();
    private int status = 200;
    private String msg = "OK";

    public HttpServletResponseImpl(Socket socket) throws IOException {
        this.socket = socket;
        this.out = socket.getOutputStream();
    }




    @Override
    public PrintWriter getWriter() {
        return printWriter;
    }

    @Override
    public OutputStream getOutputStream() {
        return out;
    }

    @Override
    public void setContentType(String type) {
        this.setHeader("Content-Type", type);
    }

    @Override
    public void setStatus(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    @Override
    public void setHeader(String name, String value) {
        headerMap.put(name, value);
    }

    @Override
    public void flushBuffer() {
        try {
            // 头行
            out.write(("HTTP/1.1 " + status + " " + msg + "\n").getBytes());
            // 头域
            headerMap.forEach((key,value)->{
                try {
                    out.write((key + ": " + value + "\n").getBytes());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            // 加入cookie Set-Cookie 发送cookie到浏览器
            /**
             * a=100; b=abc; c=...
             * 响应头存储格式
             * Set-Cookie: a=100; Max-Age=9999; path=xxxx; domain=
             * Set-Cookie: b=abc; Max-Age=9999; path=xxxx; domain=
             * Set-Cookie: c=xxx; Max-Age=9999; path=xxxx; domain=
             */
            for (Cookie cookie : cookies) {
                out.write(("Set-Cookie:" + cookie + "\n").getBytes());
            }
            // CRLF 空行
            out.write(("\n").getBytes());
            // body 正文
            String content = charArrayWriter.toString();
            out.write(content.getBytes());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void sendRedirect(String location) {
        // 只要给定 响应结果码 301 304
        setStatus(301,"Redirect");
        setHeader("Location", location);
    }

    // 临时保存的cookie集合
    Set<Cookie> cookies = new HashSet<>();

    @Override
    public void addCookie(Cookie cookie) {
        cookies.add(cookie);
    }
}
